package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    /**
     *      课程大纲列表，根据课程id查询
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2.根据课程id查询课程里面所有的小节
        //1.根据课程id查询课程里面所有的章节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId); //条件是必须和课程id相等
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //3.遍历查询章节list集合进行封装
        //创建list集合用于封装最终的数据
        List<ChapterVo> finalList = new ArrayList<>();
        //遍历查询章节的数据
        for (int i = 0; i < eduChapterList.size(); i++) {
            //获取每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //把eduChapter 对象的值赋值到chapterVo 对象里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo对象放到最终的list集合中
            finalList.add(chapterVo);


            //4.遍历小节数据，进行封装
            //创建vo对象list集合，用于封装小节的数据
            List<VideoVo> videoList = new ArrayList<>();
            for (int j = 0; j < eduVideoList.size(); j++) {
                //得到每个小节的数据
                EduVideo eduVideo = eduVideoList.get(j);
                //判断，小节里面的章节id是否和章节表中的id一样
                if (eduVideo.getChapterId().equals(chapterVo.getId())) {
                    //如果id一致再进行数据封装，否则不执行
                    VideoVo videoVo = new VideoVo();
                    //把eduVideo对象的数据放到videoVo 对象里
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //把封装过的最终数据放到小节vo对象list集合里面
                    videoList.add(videoVo);
                }
            }
            //把封装之后的小节list集合放到章节对象里面去
            chapterVo.setChildren(videoList);
        }
        return finalList; //返回章节对象集合
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节表，如果有小节不进行删除操作
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId); //小节里面对应的章节id必须相等
        int count = videoService.count(wrapper);
        if (count > 0) { //查询出小节的数据不能进行删除操作
            throw new GuliException(201,"存在小节数据不能删除");
        } else { //没有查询出小节的数据就进行删除操作
            int result = baseMapper.deleteById(chapterId);

            return result > 0; //成功1>0 失败：0>0
        }
    }

    /**
     *      根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {

        QueryWrapper<EduChapter> wra = new QueryWrapper<>();
        wra.eq("course_id",courseId);
        baseMapper.delete(wra);
    }
}
